package cn.liulin.java8.lambda;

import cn.liulin.java8.pojo.Apple;
import com.alibaba.fastjson.JSON;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

/**
 * cn.liulin.java8.lambda$
 *
 * @author ll
 * @date 2021-09-17 14:43:27
 **/
public class LambdaUse {
    public static void main(String[] args) {
        simpleSort();
        middleSort();
    }

    private static void middleSort() {
        List<Apple> listArray = new ArrayList<>();
        listArray.add(new Apple(10d));
        listArray.add(new Apple(11d));
        listArray.add(new Apple(12d));
        listArray.add(new Apple(13d));
        listArray.add(new Apple(1d));
        listArray.add(new Apple(9d));
        listArray.add(new Apple(7d));
        List<Apple> listArray2 = new ArrayList<>(listArray);
        List<Apple> listArray3 = new ArrayList<>(listArray);

        listArray.sort(new Comparator<Apple>() {
            @Override
            public int compare(Apple o1, Apple o2) {
                return o1.getWeight().compareTo(o2.getWeight());
            }
        });
        System.out.println(JSON.toJSONString(listArray));
        listArray2.sort((o1, o2) -> o1.getWeight().compareTo(o2.getWeight()));
        System.out.println(JSON.toJSONString(listArray2));
        listArray3.sort(Comparator.comparing(Apple::getWeight));
        listArray3.sort(Comparator.comparing((a) -> a.getWeight()));
        System.out.println(JSON.toJSONString(listArray3));
    }

    private static void simpleSort() {
        List<Integer> listArray = new ArrayList<>();
        listArray.add(10);
        listArray.add(1);
        listArray.add(2);
        listArray.add(4);
        listArray.add(9);
        listArray.add(15);
        List<Integer> listArray2 = new ArrayList<>(listArray);
        List<Integer> listArray3 = new ArrayList<>(listArray);
        listArray.sort(new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o1.compareTo(o2);
            }
        });
        System.out.println(listArray);
        listArray2.sort((o1, o2) -> o1.compareTo(o2));
        System.out.println(listArray2);
        listArray3.sort(Integer::compare);
        System.out.println(listArray3);

    }
}
